//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cfca.seal.front.service.ws.impl;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.OperationLog;
import cfca.seal.bean.Organization;
import cfca.seal.bean.Seal;
import cfca.seal.bean.SealImage;
import cfca.seal.dao.main.IAdminUserDao;
import cfca.seal.dao.main.IOperationLogDao;
import cfca.seal.dao.main.IOrganizationDao;
import cfca.seal.dao.main.ISealDao;
import cfca.seal.dao.main.ISealImageDao;
import cfca.seal.exception.CodeException;
import cfca.seal.front.service.FunctionType;
import cfca.seal.front.service.ws.IMakeSealService;
import cfca.seal.front.service.ws.bean.NameSealInfoBean;
import cfca.seal.front.service.ws.bean.SealInfoBean;
import cfca.seal.front.service.ws.util.ValidityUtil;
import cfca.seal.maker.util.SealMaker;
import cfca.seal.mananger.OperationLogManager;
import cfca.seal.mananger.PdfSealManager;
import cfca.seal.mananger.SealManager;
import cfca.seal.system.Env;
import cfca.seal.util.EncryptUtil;
import cfca.seal.util.ImageUtil;
import cfca.seal.util.OutPicture;
import cfca.seal.util.StringUtil;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.Date;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MakeSealServiceImpl implements IMakeSealService {
  private static final Logger log = LoggerFactory.getLogger("service_sys");
  private static final int INSERT = 0;
  private static final int UPDATE = 1;
  private static final int SEAL_TYPE_PERSON = 1;
  @Resource
  ISealDao dao;
  @Resource
  ISealImageDao modeldao;
  @Resource
  IAdminUserDao managerdao;
  @Resource
  IOrganizationDao orgdao;
  @Resource
  IOperationLogDao operationLogDao;

  public MakeSealServiceImpl() {
  }

  public String makeSeal(byte[] pfx, String pfxPassword, byte[] image, String sealInfoXML) {
    return this.doMakeSeal(pfx, pfxPassword, image, sealInfoXML, 0);
  }

  public String makeNamedSeal(byte[] pfx, String pfxPassword, String namedSealInfoXML) {
    return this.saveNameSeal(pfx, pfxPassword, namedSealInfoXML, 0);
  }

  public String updateSeal(byte[] pfx, String pfxPassword, byte[] image, String sealInfoXML) {
    return this.doMakeSeal(pfx, pfxPassword, image, sealInfoXML, 1);
  }

  public String updateNamedSeal(byte[] pfx, String pfxPassword, String namedSealInfoXML) {
    return this.saveNameSeal(pfx, pfxPassword, namedSealInfoXML, 1);
  }

  private String doMakeSeal(byte[] pfxFile, String pfxPasswd, byte[] image, String sealInfoXML, int saveorupdate) {
    OperationLog operationLog = null;
    if (0 == saveorupdate) {
      log.info("制章开始");
      log.info("制章策略：" + sealInfoXML);
      operationLog = OperationLogManager.getInstance(FunctionType.makeSeal.toString(), "普通制章");
    } else if (1 == saveorupdate) {
      log.info("更新印章开始");
      log.info("更新印章策略：" + sealInfoXML);
      operationLog = OperationLogManager.getInstance(FunctionType.updateSeal.toString(), "更新普通印章");
    }

    String orgId = "";
    StringBuffer buffer = new StringBuffer();

    String var10;
    try {
      SealInfoBean sealInfoBean = new SealInfoBean(sealInfoXML);
      operationLog.setObjectId(sealInfoBean.getSealCode());
      Seal sealInDB = null;
      if (1 == saveorupdate) {
        sealInDB = this.checkSealCode(sealInfoBean.getSealCode());
      }

      ValidityUtil.checkParamForSealInfo(sealInfoBean);
      this.checkOrgCode(sealInfoBean.getOrgCode());
      Seal seal = new Seal();
      seal.setId(StringUtil.generateID());
      seal.setName(sealInfoBean.getSealName());
      seal.setCode(sealInfoBean.getSealCode());
      seal.setSealPwd(sealInfoBean.getSealPassword());
      seal.setSealPfxData(null != pfxFile && pfxFile.length != 0 ? pfxFile : sealInDB.getSealPfxData());
      seal.setSealPfxPwd(StringUtil.isEmpty(pfxPasswd) ? EncryptUtil.decrypto(sealInDB.getSealPfxPwd()) : pfxPasswd);
      ValidityUtil.checkCertAndPassword(seal.getSealPfxData(), seal.getSealPfxPwd());
      AdminUser mng = this.managerdao.getManagerByUsername(sealInfoBean.getSealPerson());
      if (mng != null) {
        seal.setCreator(mng.getRealName());
      } else {
        seal.setCreator(sealInfoBean.getSealPerson());
      }

      Organization org = this.orgdao.getOrganizationByCode(sealInfoBean.getOrgCode());
      seal.setOrganizationId(org.getId());
      orgId = org.getId();
      if (image != null && image.length != 0) {
        ByteArrayInputStream bais = new ByteArrayInputStream(image);
        BufferedImage bImage = ImageIO.read(bais);
        int imagehighet = bImage.getHeight();
        int imagewidth = bImage.getWidth();
        this.saveNoModel(seal, ImageUtil.transferAlpha(image), imagehighet, imagewidth, Integer.parseInt(sealInfoBean.getSealType()), saveorupdate, sealInfoBean.getSealOrg());
      } else {
        ValidityUtil.checkImageCode(sealInfoBean.getImageCode());
        this.save(seal, sealInfoBean.getImageCode(), saveorupdate, sealInfoBean.getSealOrg());
      }

      if ("sqlite".equals(Env.databaseType)) {
        PdfSealManager.getInstance().addPdfSeal(seal);
        SealManager.getInstance().addSeal(seal);
      }

      log.info("制章/更新印章: 制章人:" + sealInfoBean.getSealPerson() + ",印章编码:" + sealInfoBean.getSealCode() + ",所属机构:" + sealInfoBean.getOrgCode() + ",结束");
      return buffer.append("<Result><Code>200</Code><Message>successfully!</Message></Result>").toString();
    } catch (CodeException var22) {
      operationLog.setResult(10);
      log.error("接口在进行制章发生系统异常!", var22);
      buffer.append("<Result><Code>" + var22.getCode() + "</Code><Message>" + var22.getMessage() + "</Message></Result>");
      var10 = buffer.toString();
    } catch (Exception var23) {
      operationLog.setResult(10);
      buffer.append("<Result><Code>C0001</Code><Message>接口在进行制章发生系统异常!</Message></Result>");
      log.error("接口在进行制章发生系统异常!", var23);
      var10 = buffer.toString();
      return var10;
    } finally {
      operationLog.setOrganizationId(orgId);
      this.operationLogDao.insert(operationLog);
    }

    return var10;
  }

  private boolean save(Seal seal, String imageCode, int saveorupdate, String sealOrg) throws CodeException {
    try {
      Seal aseal = this.dao.getSealByCode(seal.getCode());
      if (saveorupdate == 0) {
        if (aseal != null) {
          throw new CodeException("C1008", "重复的印章编码");
        }
      } else if (aseal == null) {
        throw new CodeException("C4004", "没有找到对应的印章");
      }

      SealImage model = this.modeldao.getSealModelByModelCode(imageCode);
      if (model == null) {
        throw new CodeException("C3056", "印模不存在");
      } else {
        seal.setType(model.getType());
        seal.setSealImageData(model.getSealImageData());
        seal.setImageHeight(model.getImageHeight());
        seal.setImageWidth(model.getImageWidth());
        return this.insertToDB(seal, saveorupdate, sealOrg);
      }
    } catch (CodeException var7) {
      throw new CodeException(var7.getCode(), var7.getMessage());
    } catch (Exception var8) {
      throw new CodeException("C0001", var8);
    }
  }

  private boolean insertToDB(Seal seal, int saveorupdate, String sealOrg) throws CodeException {
    try {
      if (seal.getSealPfxData() != null && seal.getSealPfxData().length > 0) {
        try {
          seal.setCreateTime(new Date());
          SealMaker maker = new SealMaker(seal.getSealPfxData(), seal.getSealPfxPwd(), seal.getSealImageData(), seal.getCreator(), sealOrg, seal.getName());
          byte[] sealData = maker.createSeal();
          seal.setSealData(sealData);
          seal.setSealPfxPwd(EncryptUtil.encrypto(seal.getSealPfxPwd()));
          seal.setSealPwd(EncryptUtil.encrypto(seal.getSealPwd()));
          seal.setStatus(1);
        } catch (Exception var6) {
          log.error("制作印章发生异常", var6);
          throw new CodeException("C0001", "制作印章发生异常", var6);
        }
      }

      return saveorupdate == 0 ? this.dao.insert(seal) : this.dao.update(seal);
    } catch (Exception var7) {
      throw new CodeException("C0001", var7);
    }
  }

  private boolean saveNoModel(Seal seal, byte[] image, int height, int width, int type, int saveorupdate, String sealOrg) throws CodeException {
    try {
      Seal aseal = this.dao.getSealByCode(seal.getCode());
      if (saveorupdate == 0) {
        if (aseal != null) {
          throw new CodeException("C1008", "重复的印章编码");
        }
      } else if (aseal == null) {
        throw new CodeException("C4004", "没有找到对应的印章");
      }

      seal.setType(type);
      seal.setSealImageData(image);
      seal.setImageHeight(height);
      seal.setImageWidth(width);
      return this.insertToDB(seal, saveorupdate, sealOrg);
    } catch (CodeException var9) {
      throw new CodeException(var9.getCode(), var9.getMessage());
    } catch (Exception var10) {
      throw new CodeException("C0001", var10);
    }
  }

  private void checkOrgCode(String orgCode) throws Exception {
    Organization org = this.orgdao.getOrganizationByCode(orgCode);
    if (org == null) {
      throw new CodeException("C8002", "机构编码不存在");
    }
  }

  private Seal checkSealCode(String sealCode) throws Exception {
    ValidityUtil.checkSealCode(sealCode);
    Seal sealForUpdate = this.dao.getSealByCode(sealCode);
    if (null == sealForUpdate) {
      throw new CodeException("C4004", "没有找到对应的印章");
    } else if (1 != sealForUpdate.getStatus()) {
      throw new CodeException("C4005", "印章未启用");
    } else {
      return sealForUpdate;
    }
  }

  private String saveNameSeal(byte[] pfxFile, String pfxPasswd, String sealInfoXML, int saveorupdate) {
    OperationLog operationLog = null;
    if (0 == saveorupdate) {
      log.info("制作更新人名章，开始");
      log.info("制作人名章策略：" + sealInfoXML);
      operationLog = OperationLogManager.getInstance(FunctionType.makeNamedSeal.toString(), "制作人名章");
    } else if (1 == saveorupdate) {
      log.info("更新人名章，开始");
      log.info("更新人名章策略：" + sealInfoXML);
      operationLog = OperationLogManager.getInstance(FunctionType.updateNamedSeal.toString(), "更新人名章");
    }

    String orgId = "";
    StringBuffer buffer = new StringBuffer();

    String var9;
    try {
      NameSealInfoBean nameSealInfoBean = new NameSealInfoBean(sealInfoXML);
      operationLog.setObjectId(nameSealInfoBean.getSealCode());
      Seal sealInDB = null;
      if (1 == saveorupdate) {
        sealInDB = this.checkSealCode(nameSealInfoBean.getSealCode());
      }

      ValidityUtil.checkParamForNameSealInfo(nameSealInfoBean);
      this.checkOrgCode(nameSealInfoBean.getOrgCode());
      Seal seal = new Seal();
      seal.setId(StringUtil.generateID());
      seal.setName(nameSealInfoBean.getSealName());
      seal.setCode(nameSealInfoBean.getSealCode());
      seal.setSealPwd(nameSealInfoBean.getSealPassword());
      seal.setSealPfxPwd(StringUtil.isEmpty(pfxPasswd) ? EncryptUtil.decrypto(sealInDB.getSealPfxPwd()) : pfxPasswd);
      seal.setSealPfxData(null != pfxFile && pfxFile.length != 0 ? pfxFile : sealInDB.getSealPfxData());
      ValidityUtil.checkCertAndPassword(seal.getSealPfxData(), seal.getSealPfxPwd());
      AdminUser mng = this.managerdao.getManagerByUsername(nameSealInfoBean.getSealPerson());
      if (mng != null) {
        seal.setCreator(mng.getRealName());
      } else {
        seal.setCreator(nameSealInfoBean.getSealPerson());
      }

      Organization org = this.orgdao.getOrganizationByCode(nameSealInfoBean.getOrgCode());
      seal.setOrganizationId(org.getId());
      orgId = org.getId();
      byte[] image = "".getBytes();
      int imageHeight = Integer.valueOf(nameSealInfoBean.getImageHeight());
      int imageWidth = Integer.valueOf(nameSealInfoBean.getImageWidth());
      int fontSize = Integer.valueOf(nameSealInfoBean.getFontSize());
      if ("1".equals(nameSealInfoBean.getImageShape())) {
        image = OutPicture.getNamePngToByte(nameSealInfoBean.getSealName(), imageHeight, imageWidth, fontSize, nameSealInfoBean.getColor());
      } else if ("3".equals(nameSealInfoBean.getImageShape())) {
        image = OutPicture.getRectanglePng(nameSealInfoBean.getSealName(), imageWidth, imageHeight, fontSize, nameSealInfoBean.getColor());
      }

      this.saveNoModel(seal, ImageUtil.transferAlpha(image), imageHeight, imageWidth, 1, saveorupdate, nameSealInfoBean.getSealOrg());
      if ("sqlite".equals(Env.databaseType)) {
        PdfSealManager.getInstance().addPdfSeal(seal);
        SealManager.getInstance().addSeal(seal);
      }

      log.info("制作/更新人名章: 制章人:" + nameSealInfoBean.getSealPerson() + ",印章编码:" + nameSealInfoBean.getSealCode() + ",所属机构:" + nameSealInfoBean.getOrgCode() + ",结束");
      return "<Result><Code>200</Code><Message>successfully!</Message></Result>";
    } catch (CodeException var21) {
      operationLog.setResult(10);
      log.error("接口在进行制人名章发生系统异常! ", var21);
      buffer.append("<Result><Code>" + var21.getCode() + "</Code><Message>" + var21.getMessage() + "</Message></Result>");
      var9 = buffer.toString();
      return var9;
    } catch (Exception var22) {
      operationLog.setResult(10);
      buffer.append("<Result><Code>C0001</Code><Message>接口在进行制人名章发生系统异常!</Message></Result>");
      log.error("接口在进行制人名章发生系统异常! ", var22);
      var9 = buffer.toString();
    } finally {
      operationLog.setOrganizationId(orgId);
      this.operationLogDao.insert(operationLog);
    }

    return var9;
  }
}
